/**
 * 建筑系统 - 俯视图房屋渲染
 */

// 建筑基类
class Building {
    constructor(x, y, width, height, type = 'building') {
        this.position = new Vector2D(x, y);
        this.size = new Vector2D(width, height);
        this.type = type;
        this.destructible = false;
        this.maxHealth = 100;
        this.health = this.maxHealth;
        this.destroyed = false;
        
        // 房屋特征
        this.roofColor = '#8b7355';
        this.wallColor = '#a0522d';
        this.windowPositions = [];
        this.doorPosition = null;
        this.chimneyPosition = null;
        
        this.generateHouseFeatures();
    }
    
    generateHouseFeatures() {
        const w = this.size.x;
        const h = this.size.y;
        
        // 随机生成窗户位置
        this.windowPositions = [];
        if (w > 60 && h > 60) {
            this.windowPositions.push(
                {x: w * 0.2, y: h * 0.3, size: 8},
                {x: w * 0.8, y: h * 0.3, size: 8},
                {x: w * 0.2, y: h * 0.7, size: 8},
                {x: w * 0.8, y: h * 0.7, size: 8}
            );
        } else if (w > 40 && h > 40) {
            this.windowPositions.push(
                {x: w * 0.3, y: h * 0.4, size: 6},
                {x: w * 0.7, y: h * 0.6, size: 6}
            );
        }
        
        // 烟囱位置
        if (Math.random() > 0.4) {
            this.chimneyPosition = {
                x: w * (0.2 + Math.random() * 0.6),
                y: h * (0.2 + Math.random() * 0.6),
                size: Math.min(w, h) * 0.1
            };
        }
        
        // 门的位置
        const doorSide = Math.floor(Math.random() * 4);
        switch (doorSide) {
            case 0: // 上
                this.doorPosition = {x: w * 0.5, y: 0, width: 8, height: 4, side: 'top'};
                break;
            case 1: // 下
                this.doorPosition = {x: w * 0.5, y: h - 4, width: 8, height: 4, side: 'bottom'};
                break;
            case 2: // 左
                this.doorPosition = {x: 0, y: h * 0.5, width: 4, height: 8, side: 'left'};
                break;
            case 3: // 右
                this.doorPosition = {x: w - 4, y: h * 0.5, width: 4, height: 8, side: 'right'};
                break;
        }
    }
    
    takeDamage(damage) {
        if (!this.destructible) return false;
        
        this.health -= damage;
        if (this.health <= 0) {
            this.destroyed = true;
            return true;
        }
        return false;
    }
    
    render(ctx) {
        if (this.destroyed) return;
        
        ctx.save();
        ctx.translate(this.position.x, this.position.y);
        
        this.renderRoofBase(ctx);
        this.renderRoofDetails(ctx);
        this.renderWindows(ctx);
        this.renderChimney(ctx);
        this.renderRoofEdges(ctx);
        
        ctx.restore();
    }
    
    renderRoofBase(ctx) {
        ctx.fillStyle = this.roofColor;
        ctx.fillRect(0, 0, this.size.x, this.size.y);
    }
    
    renderRoofDetails(ctx) {
        const panelSize = 40;
        const rows = Math.floor(this.size.y / panelSize);
        const cols = Math.floor(this.size.x / panelSize);
        
        ctx.strokeStyle = '#555555';
        ctx.lineWidth = 1;
        
        for (let row = 1; row < rows; row++) {
            ctx.beginPath();
            ctx.moveTo(0, row * panelSize);
            ctx.lineTo(this.size.x, row * panelSize);
            ctx.stroke();
        }
        
        for (let col = 1; col < cols; col++) {
            ctx.beginPath();
            ctx.moveTo(col * panelSize, 0);
            ctx.lineTo(col * panelSize, this.size.y);
            ctx.stroke();
        }
    }
    
    renderWindows(ctx) {
        for (const window of this.windowPositions) {
            ctx.fillStyle = 'rgba(135, 206, 250, 0.8)';
            ctx.fillRect(window.x - window.size/2, window.y - window.size/2, window.size, window.size);
            
            ctx.strokeStyle = '#654321';
            ctx.lineWidth = 1;
            ctx.strokeRect(window.x - window.size/2, window.y - window.size/2, window.size, window.size);
            
            ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
            ctx.fillRect(window.x - window.size/2, window.y - window.size/2, window.size/2, window.size/2);
        }
    }
    
    renderChimney(ctx) {
        if (!this.chimneyPosition) return;
        
        const chimney = this.chimneyPosition;
        
        ctx.fillStyle = '#8b4513';
        ctx.fillRect(chimney.x - chimney.size, chimney.y - chimney.size, chimney.size * 2, chimney.size * 2);
        
        ctx.fillStyle = '#654321';
        ctx.fillRect(chimney.x - chimney.size - 1, chimney.y - chimney.size - 1, chimney.size * 2 + 2, 2);
    }
    
    renderRoofEdges(ctx) {
        ctx.strokeStyle = 'rgba(0, 0, 0, 0.3)';
        ctx.lineWidth = 2;
        ctx.strokeRect(0, 0, this.size.x, this.size.y);
        
        ctx.strokeStyle = 'rgba(255, 255, 255, 0.2)';
        ctx.lineWidth = 1;
        ctx.strokeRect(1, 1, this.size.x - 2, this.size.y - 2);
    }
}

// 木制房屋 - 可破坏
class WoodenHouse extends Building {
    constructor(x, y, width, height) {
        super(x, y, width, height, 'wooden');
        this.destructible = true;
        this.maxHealth = 75;
        this.health = this.maxHealth;
        
        this.roofColor = '#8b4513';
        this.tileColor = '#a0522d';
        this.wallColor = '#deb887';
    }
    
    renderRoofBase(ctx) {
        const gradient = ctx.createLinearGradient(0, 0, this.size.x, this.size.y);
        gradient.addColorStop(0, '#d2691e');
        gradient.addColorStop(0.3, this.roofColor);
        gradient.addColorStop(0.7, this.roofColor);
        gradient.addColorStop(1, '#654321');
        
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, this.size.x, this.size.y);
    }
    
    renderRoofDetails(ctx) {
        const tileSize = 16;
        const rows = Math.floor(this.size.y / tileSize);
        const cols = Math.floor(this.size.x / tileSize);
        
        ctx.strokeStyle = '#654321';
        ctx.lineWidth = 0.5;
        
        for (let row = 1; row < rows; row++) {
            ctx.beginPath();
            ctx.moveTo(0, row * tileSize);
            ctx.lineTo(this.size.x, row * tileSize);
            ctx.stroke();
        }
        
        for (let col = 1; col < cols; col++) {
            ctx.beginPath();
            ctx.moveTo(col * tileSize, 0);
            ctx.lineTo(col * tileSize, this.size.y);
            ctx.stroke();
        }
    }
}

// 混凝土房屋 - 不可破坏
class ConcreteHouse extends Building {
    constructor(x, y, width, height) {
        super(x, y, width, height, 'concrete');
        this.destructible = false;
        
        this.roofColor = '#696969';
        this.accentColor = '#888888';
        this.wallColor = '#a9a9a9';
    }
    
    renderRoofBase(ctx) {
        const gradient = ctx.createLinearGradient(0, 0, this.size.x, this.size.y);
        gradient.addColorStop(0, '#778899');
        gradient.addColorStop(0.5, this.roofColor);
        gradient.addColorStop(1, '#2f4f4f');
        
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, this.size.x, this.size.y);
    }
    
    renderRoofDetails(ctx) {
        const panelSize = 40;
        
        ctx.strokeStyle = '#555555';
        ctx.lineWidth = 1;
        
        if (this.size.x > panelSize) {
            ctx.beginPath();
            ctx.moveTo(this.size.x / 2, 0);
            ctx.lineTo(this.size.x / 2, this.size.y);
            ctx.stroke();
        }
        
        if (this.size.y > panelSize) {
            ctx.beginPath();
            ctx.moveTo(0, this.size.y / 2);
            ctx.lineTo(this.size.x, this.size.y / 2);
            ctx.stroke();
        }
    }
} 